<template>
  <div id="app" class="chat-container">
    
    <h1 class="title">
        <img class="avatar_1"  src="https://img.alicdn.com/imgextra/i4/O1CN01EfJVFQ1uZPd7W4W6i_!!6000000006051-2-tps-112-112.png" alt="" >
        通义千问
    </h1>
    <div class="chat-box" ref="chatBox">
        <div v-for="(message, index) in messages" :key="index" :class="['message', message.role]">
            <img v-if="message.role === 'user'" src="https://img.alicdn.com/imgextra/i2/O1CN01tP9oqA1YCaH2us3WV_!!6000000003023-55-tps-32-32.svg" alt="User Avatar" class="avatar" />
            <img v-if="message.role === 'assistant'" src="https://img.alicdn.com/imgextra/i4/O1CN01EfJVFQ1uZPd7W4W6i_!!6000000006051-2-tps-112-112.png" alt="AI Avatar" class="avatar" />
            <p>{{ message.content }}</p>
        </div>
    </div>
    <div class="input-area">
      <input v-model="userInput" @keyup.enter="sendMessage" placeholder="请输入你的问题..." />
      <button @click="sendMessage">发送</button>
    </div>
  </div>
</template>

<script>
import OpenAI from "openai";

export default {
  data() {
    return {
      userInput: "",
      messages: [],
      openai: new OpenAI({
        apiKey: "sk-cfe57233e5cc42ef8c6f4e792f831947",
        baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
        dangerouslyAllowBrowser: true
      })
    };
  },
  methods: {
    async sendMessage() {
      if (!this.userInput.trim()) return;
      this.messages.push({ role: "user", content: this.userInput });
      let userMessage = this.userInput;
      this.userInput = "";
      this.$nextTick(() => this.scrollToBottom());

      try {
        const completion = await this.openai.chat.completions.create({
          model: "qwen-plus",
          messages: [
            { role: "system", content: "You are a helpful assistant." },
            ...this.messages
          ],
          stream: true
        });
        
        let botResponse = '';
        for await (const part of completion) {
          botResponse += part.choices[0]?.delta?.content || '';
        }
        this.messages.push({ role: "assistant", content: botResponse });
        this.$nextTick(() => this.scrollToBottom());
      } catch (error) {
        console.error("Error fetching AI response:", error);
        this.messages.push({ role: "assistant", content: "抱歉，获取回复时出错。" });
        this.$nextTick(() => this.scrollToBottom());
      }
    },
    scrollToBottom() {
      this.$refs.chatBox.scrollTop = this.$refs.chatBox.scrollHeight;
    }
  }
};
</script>
<style scoped>
.chat-container {
  max-width: 1200px;
  margin: 50px auto;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.title {
  text-align: center;
  font-size: 40px;
  font-weight: bold;
  color: #333;
  margin-bottom: 15px;
}

.chat-box {
  height: 750px;
  overflow-y: auto;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #f9f9f9;
}

.message {
  position: relative;
  margin-left: 10px;
  max-width: 80%;
  padding: 10px;
  border-radius: 10px;
  margin: 15px 0; 
  line-height: 1.5;
  word-break: break-word;
  display: flex;
  align-items: flex-start;
  display: flex;
  flex-direction: row;
  margin: 15px auto; /* 居中对齐 */
}

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-right: 10px;
}


.avatar_1 {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    margin-right: 10px;
}
.user {
  align-self: flex-end;
  background: #0084ff;
  color: white;
  flex-direction: row-reverse; /* 翻转布局，头像在右侧 */

}

.user .avatar {
  margin-right: 0;
  margin-left: 10px; /* 调整头像的间距 */
}

.assistant {
  align-self: flex-start;
  background: #e5e5ea;
  color: black;
}

.input-area {
  display: flex;
  margin-top: 10px;
}

input {
  height: 40px;
  flex: 1;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 5px;
  font-size: 14px;
}

button {
  padding: 12px 16px;
  background: #0084ff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  margin-left: 10px;
}

button:hover {
  background: #0070e0;
}
</style>